package com.chidopi.market.util;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.Globals;
import org.apache.struts.action.ActionMessage;
import org.apache.struts.action.ActionMessages;
import org.apache.struts.config.ModuleConfig;
import org.apache.struts.util.MessageResources;
import org.apache.struts.util.ModuleUtils;

public class MessageHandler {
		
	public static MessageHandler getInstance(){		
        return messageHandler;
    }
	
	public void processMessage(HttpServletRequest request, String messageKey){
	    processMessage(request, messageKey, null);
	}

	public void processMessage(HttpServletRequest request, String messageKey,
			Object[] args) {		
		 processMessage(request,messageKey, messageKey, null);
	}

	private void processMessage(HttpServletRequest request, String property,
			String messageKey, Object[] args) {
	    process(request, Globals.MESSAGE_KEY, property, messageKey, args);		
		
	}
	
	public void processError(HttpServletRequest request, String messageKey){
		processError(request, messageKey, null);
	}

	public void processError(HttpServletRequest request, String messageKey,
			Object[] args) {		
		processError(request,messageKey, messageKey, null);
	}

	public String getResourceMessage(HttpServletRequest request,String messageKey) {
		return getResourceMessage(request,null, messageKey, null);
	}
	
	public String getResourceMessage(HttpServletRequest request, String messageKey, Object[] args) {
		
		return getResourceMessage(request, null, messageKey,args);
	}
	
	public String getResourceMessage(HttpServletRequest request,String bundle, String messageKey){
		return getResourceMessage(request, bundle, messageKey,null);
	}
	
	public String getResourceMessage(HttpServletRequest request, String bundle, String messageKey, Object[] args){
        MessageResources res = getMessageResources(request,bundle);
        return res.getMessage(messageKey,args);
	}
	
	public void processAjaxMessage(HttpServletResponse response, String message) {
		PrintWriter writer;
		try {
			writer = response.getWriter();
			writer.write(message);
			writer.flush();
			writer.close();
		} catch (IOException e) {
			e.printStackTrace();
		}		
	}
	/*-----------------  private members bellow -------------------------*/
	

	private static MessageHandler messageHandler  = new MessageHandler();
	
	private MessageHandler(){}
	
	private ActionMessages messages;
	
	
	private void processError(HttpServletRequest request, String property,
			String messageKey, Object[] args) {
	    process(request, Globals.ERROR_KEY, property, messageKey, args);		
	}
	
	private void process(HttpServletRequest request, String messageType, String property, String messageKey, Object [] args){
		messages = new ActionMessages();
		messages.add(property, new ActionMessage(messageKey,args));
		request.setAttribute(messageType, messages);
	}
		
	private MessageResources getMessageResources(HttpServletRequest request, String bundle){
		MessageResources res ;
		if(bundle == null){
			res = (MessageResources) request.getAttribute(Globals.MESSAGES_KEY);
		}else{
			 // Identify the current module
	        ServletContext context = request.getSession(false).getServletContext();
	        ModuleConfig moduleConfig = ModuleUtils.getInstance().getModuleConfig(request, context);

	        // Return the requested message resources instance
	        res =  (MessageResources) context.getAttribute(bundle + moduleConfig.getPrefix());
		}
		
		return res;
		
	}
}
